﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Media;

namespace DataForest.DataModel
{
    public class Workspace
    {
        public Workspace()
        {
            this.Groups.CollectionChanged += HandleGroupsCollectionChanged;
        }

        private static Brush[] brushes = new Brush[] { Brushes.LightCyan, Brushes.LightGoldenrodYellow, Brushes.Honeydew, Brushes.FloralWhite, Brushes.Moccasin };
        private Queue<Brush> brushQueue = new Queue<Brush>(brushes);

        public event NotifyCollectionChangedEventHandler WorkgroupTreeCollectionChanged;

        public event NotifyCollectionChangedEventHandler WorkgroupCollectionChanged;

        public ObservableCollection<Workgroup> Groups = new ObservableCollection<Workgroup>();

        public Workgroup AddWorkgroup()
        {
            Workgroup group = new Workgroup();
            group.BackgroundBrush = brushQueue.Dequeue();
            brushQueue.Enqueue(group.BackgroundBrush);
            this.Groups.Add(group);
            return group;
        }

        public Workgroup AddWorkgroup(Table table)
        {

            Workgroup group = new Workgroup(table);
            group.BackgroundBrush = brushQueue.Dequeue();
            brushQueue.Enqueue(group.BackgroundBrush);
            this.Groups.Add(group);
            return group;
        }

        public void AddInteractiveTree(Workgroup group)
        {
            
            Tree tree = new Tree(group.Table);
            group.Trees.Add(tree);
            tree.Name = tree.Table.Name + " (Interaktiv)";
        }

        public void AddOptimalTree(Workgroup group)
        {
            
            Tree tree = new Tree(group.Table);
            tree.CreateOptimalTree();
            group.Trees.Add(tree);
            tree.Name = tree.Table.Name + " (Auto)";
        }

        public void RemoveTree(Workgroup group, Tree tree)
        {
            group.Trees.Remove(tree);
        }

        public void RemoveWorkgroup(Workgroup group)
        {
            Groups.Remove(group);
        }

        public Workgroup LoadCSV(string file, string seperator)
        {
            List<string> errors = new List<string>();
            Table table = Table.ReadCSV(file, seperator, out errors);
            table.FileName = Path.GetFileName(file);
            Workgroup group = this.AddWorkgroup(table);

            return group;

        }

        public Workgroup LoadExcel(string file, int mappe, bool firstRowColumnNames)
        {
            Table table = Table.LoadExcel(file, firstRowColumnNames);
            Workgroup group = this.AddWorkgroup(table);
            return group;

        }

        public void SaveCSV(Workgroup group, string file, string seperator)
        {
            group.Table.CreateCSVFile(file, seperator);
        }

        public void SaveExcel(Workgroup group, string file, bool firstRowColumnNames)
        {
            group.Table.CreateExcel(file, firstRowColumnNames);
        }


        private void HandleGroupsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            NotifyCollectionChangedEventHandler handler = WorkgroupCollectionChanged;
            if (handler != null && e.NewItems != null)
            {
                foreach (Workgroup group in e.NewItems)
                {
                    handler(group, e);
                }
            }

            if (handler != null && e.OldItems != null)
            {
                foreach (Workgroup group in e.OldItems)
                {
                    handler(group, e);
                }
            }

            // if new Group Added Attach a Listener
            if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems != null)
            {

                foreach (Workgroup group in e.NewItems)
                {
                    group.TreeCollectionChanged += HandleWorkgroupChanged;
                }
            }

            // if an item was removed dettach the listener
            if (e.Action == NotifyCollectionChangedAction.Remove && e.OldItems != null)
            {
                foreach (Workgroup group in e.OldItems)
                {
                    group.TreeCollectionChanged -= HandleWorkgroupChanged;
                }
            }
        }


        private void HandleWorkgroupChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            NotifyCollectionChangedEventHandler handler = WorkgroupTreeCollectionChanged;

            System.Diagnostics.Debug.WriteLine("Workspace: Fire WorkgroupChanged");
            if (handler != null)
            {
                handler(sender, e);
            }
        }

    }
}

